package com.tansun.easycare.rule.datasource.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.service.impl.CrudServiceImpl;
import com.tansun.easycare.rule.datamodel.entity.RuleEnum;
import com.tansun.easycare.rule.datamodel.service.RuleEnumService;
import com.tansun.easycare.rule.datasource.service.RuleDatasourceService;
import com.tansun.easycare.rule.deployonline.entity.RuleEnterpoint;
import com.tansun.easycare.rule.front.dao.RuleDatasourceDao;
import com.tansun.easycare.rule.front.dao.RuleEnterpointParamDao;
import com.tansun.easycare.rule.front.entity.EnterpointVo;
import com.tansun.easycare.rule.front.entity.RuleDatasource;
import com.tansun.easycare.rule.front.entity.RuleEnterpointParam;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;

/**
 * 数据源ServiceImpl
 * @author WYH
 * @version 2018-05-31
 */
@Service("ruleDatasourceService")
@Transactional(readOnly = true)
public class RuleDatasourceServiceImpl extends CrudServiceImpl<RuleDatasourceDao, RuleDatasource> implements RuleDatasourceService{

	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RuleEnterpointParamDao enterParamDao;
	@Autowired
	private RuleEnumService enumService;
    @Override
    public PageInfo<RuleDatasource> findDataSource(RuleDatasource condition){
        PageInfo<RuleDatasource> pageInfo = new PageInfo<>(this.dao.findList(condition));
        return pageInfo;
    }
	public void saveEnterParam(EnterpointVo vo) {
		enterParamDao.deleteByEnterCode(vo.getProjectCode(), vo.getCode());
		for(RuleEnterpointParam param :vo.getEnterParams()){
			if(param.getDsTable()==null||"".equals(param.getDsTable())){
				continue;
			}
			param.setProjectCode(vo.getProjectCode());
			param.setEnterCode(vo.getCode());
			param.setParamType("in");
			param.preInsert();
			enterParamDao.insert(param);
		}
		
	}
	
	public Map<String,RuleEnterpointParam> findParamMap(RuleEnterpoint enterpoint){
		RuleEnterpointParam param = new RuleEnterpointParam();
		param.setProjectCode(enterpoint.getProjectCode());
		param.setEnterCode(enterpoint.getCode());
		List<RuleEnterpointParam> list = enterParamDao.findList(param);
		Map<String,RuleEnterpointParam> paramsMap = new HashMap();
		for(RuleEnterpointParam e:list){
			paramsMap.put(e.getDsDataparam(), e);
		}
		return paramsMap;
	}
	public RuleDatasource get(String id) {
		return super.get(id);
	}
	
	public List<RuleDatasource> findList(RuleDatasource ruleDatasource) {
		return super.findList(ruleDatasource);
	}
	
	public PageInfo<RuleDatasource> findPage(Page<RuleDatasource> page, RuleDatasource ruleDatasource) {
		return super.findPage(page, ruleDatasource);
	}
	
	@Transactional(readOnly = false)
	public void save(RuleDatasource ruleDatasource) {
		super.save(ruleDatasource);
	}
	
	@Transactional(readOnly = false)
	public void delete(RuleDatasource ruleDatasource) {
		super.delete(ruleDatasource);
	}

	@Override
	public void saveEnum(Map<String, String> param) throws Exception {
		RuleDatasource ruleDatasource = this.get(param.get("datasource"));
		String sql = "select "+param.get("nameCol")+","+param.get("codeCol")+" from "+param.get("dbTable");
		if("oracle".equalsIgnoreCase(ruleDatasource.getDsType())){
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}else if("mysql".equalsIgnoreCase(ruleDatasource.getDsType())){
			Class.forName("com.mysql.jdbc.Driver");
		}else{
			log.warn("不支持的数据库类型:"+ruleDatasource.getDsType());
			return ;
		}
		try(Connection connection = DriverManager.getConnection(ruleDatasource.getDsUrl(), ruleDatasource.getDsUsername(), ruleDatasource.getDsPwd());){
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			List<RuleEnum> list = new ArrayList<RuleEnum>();
			while(rs.next()){
				RuleEnum e = new RuleEnum();
				e.setEnumName(rs.getString(1));
				e.setEnumCode(rs.getString(2));
				e.setEnumType(param.get("enumType"));
				list.add(e);
			}
			enumService.insertList(list);
		}
	}
	
}